package com.mumulx.check.web.controller;

import com.mumulx.check.model.UserInfo;
import com.mumulx.check.redis.RedisKeyType;
import com.mumulx.check.redis.RedisUtil;
import com.mumulx.check.web.util.VerifyCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

/*
 *  Created by IntelliJ IDEA.
 *  User: 木木
 *  Date: 2020/10/20
 *  Time: 21:19
 */
@Controller()
@RequestMapping("/")
@Slf4j
public class HelpDetvController {
    @Resource
    RedisUtil redisUtil;

    /* 获取验证码图片*/
    @GetMapping("/help/getVerifyCode")
    @ResponseBody
    public void getVerificationCode(HttpServletResponse response, HttpServletRequest request) {
        try {
            int width = 70;
            int height = 20;

            BufferedImage verifyImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//生成对应宽高的初始图片
            String randomText = VerifyCode.drawRandomText(width, height, verifyImg);//单独的一个类方法，出于代码复用考虑，进行了封装。功能是生成验证码字符并加上噪点，干扰线，返回值为验证码字符
            request.getSession().setAttribute("CKECKCODE", randomText);
            response.setContentType("image/png");//必须设置响应内容类型为图片，否则前台不识别
            //禁止缓存，防止验证码过期
            response.setHeader("Pragma","no-cache");
            response.setHeader("Cache-Control","no-cache");
            response.setHeader("Expires","0");

            OutputStream os = response.getOutputStream(); //获取文件输出流
            ImageIO.write(verifyImg, "png", os);//输出图片流
            os.flush();
            os.close();//关闭流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /* 获取验证码图片*/
  /*  @GetMapping("/help/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user_name");
        return "redirect:/";
    }*/
    /* 跳转到userInfo.html页面*/
    @GetMapping("/index")
    public String index(HttpServletRequest request) {

        //log.info("页面跳转---index");
        return "index";
    }

    /* 跳转到fileupload.html页面*/
    @GetMapping("/fileupload")
    public String fileupload(HttpServletRequest request) {
        return "fileupload/fileupload";
    }
    /* 跳转到testrecord.html页面*/
    @GetMapping("/testrecord")
    public String testrecord(HttpServletRequest request) {
        return "testrecord/testrecord";
    }
    /* 跳转到testrecord.html页面*/
    @PostMapping("/testresult")
    public String testresult(String recordId, Model model,HttpServletRequest request) {
        model.addAttribute("recordId",recordId);
        return "testresult/testresult";
    }
    /* 跳转到login页面*/
    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        request.getSession().setAttribute(RedisKeyType.TALK_LOVE.getKey(),redisUtil.get(RedisKeyType.TALK_LOVE.getKey()));
        return "login";
    }
    /* 跳转到503异常页面---同一账号异地登录*/
    @RequestMapping("/error/503")
    public String error503(HttpServletRequest request) {

        return "error/503";
    }
    /* 跳转到404异常页面*/
    @RequestMapping("/error/404")
    public String error404(HttpServletRequest request) {

        return "error/404";
    }
    /* 跳转到500异常页面--后台出错*/
    @RequestMapping("/error/500")
    public String error500(HttpServletRequest request) {

        return "error/500";
    }
    /* 跳转到403权限不足异常页面*/
    @RequestMapping("/error/403")
    public String unauth(HttpServletRequest request) {

        return "error/405";
    }







}
